import boto3

# export data from dynamodb to json
# command line: aws dynamodb scan --table-name yelp-restaurants > export.json



client = boto3.client('dynamodb')
data = client.scan(
    TableName='yelp-restaurants',
    Select='ALL_ATTRIBUTES',
)

id = 0

res=[]
for e in data['Items']:
    tmp = {}
    tmp["cuisine"] = e['cuisine']["S"]
    tmp["restaurantID"] = e['business_id']["S"]
    cons = {"index": {"_index": "restaurants", "_type": "Restaurant","_id":str(id)}}
    res.append(cons)
    id += 1
    res.append(tmp)

import json
with open('./final.json','w') as fp:
    for e in res:
        json.dump(e,fp)
        fp.write('\n')

# elasticsearch upload command
# curl -H "Content-Type:application/x-ndjson" --user tt2756:maDrid980305# -XPOST https://search-restaurants-m4dtizgfgazswpzlr3kiji25wq.us-east-1.es.amazonaws.com/_bulk --data-binary @final.json
